#include "BezierCurve.h"
#include <math.h>

int generateBezierCurve(vector3 p0, vector3 p1, vector3 p2, vector3 p3, vector3 * curve )
{
	int pointCount = 0;
	/**
	 * My favorite mathematician at the moment
	 * http://www.moshplant.com/direct-or/bezier/math.html
	 */

	double t = 0;
	vector3 *temp = new vector3[ (int)total_T ];

	double cx = 3 * ( p1.x - p0.x );
	double bx = 3 * ( p2.x - p1.x ) - cx;
	double ax = p3.x - p0.x - cx - bx;

	double cy = 3 * ( p1.y - p0.y );
	double by = 3 * ( p2.y - p1.y ) - cy;
	double ay = p3.y - p0.y - cy - by;

	for( int i = 0; i < total_T; i++ ){
		t = ((double)i)/total_T;
		double Tx = ax * ( t * t * t ) + bx * ( t * t ) + cx * t + p0.x;
		double Ty = ay * ( t * t * t ) + by * ( t * t ) + cy * t + p0.y;
		double Tz = 0;

		curve[ i ] = vector3( Tx, Ty, Tz );
		pointCount++;
	}

	//curve = temp;
	return pointCount;
}